/*
 * bootstrap table 跨页选择工具
 * 注意事项: 列表返回的数据请带有一个别名为 id 的id, 别名为 name 的name。
 *             数据传入时，需要id和name配对传入
 *             记得在bootstrap table设置getResponseHandler
 * auther : "zhaoshu",
 *
 * Date: 2017-05-22
 *
 */
var HFAddressUtil = {
    URL: {
        getArealist: function () {
            return "/HFDataExchange/service/area/getArealist.do";
        },
        treelist: function () {
            return "/HFDataExchange/service/area/treelist.do";
        },
        getProvince: function () {
            return "/HFDataExchange/service/area/getProvince.do";
        },
        getCityList: function () {
            return "/HFDataExchange/service/area/getCityList.do";
        },
        getSubcityList: function () {
            return "/HFDataExchange/service/area/getSubcityList.do";
        },
    },
    params: new Object(),
    init: function (china_tree_code, province_id, city_id, subcity_id) {   //省份, 市, 区 的 select_id
        // var toptree_code = "${AreaType.CHINA.getKey() }";
        var toptree_code = china_tree_code;
        HFAddressUtil.params.province_id = province_id;
        HFAddressUtil.params.city_id = city_id;
        HFAddressUtil.params.subcity_id = subcity_id;
        HFAddressUtil.params.toptree_code = toptree_code;

        //初始化数据
        HFAddressUtil.loadProvince(HFAddressUtil.selectValue).done(function () {
            HFAddressUtil.loadCity(HFAddressUtil.selectValue).done(function () {
                HFAddressUtil.loadSubcity(HFAddressUtil.selectValue);
            });
        });
        // HFAddressUtil.initProvinceCitySubcity();

        $("#" + province_id).change(function () {
            HFAddressUtil.loadCity();
            HFAddressUtil.loadSubcity();
        });
        $("#" + city_id).change(function () {
            HFAddressUtil.loadSubcity();
        });
    },
    loadProvince: function (attachFunction) { //加载省份
        var toptree_code = HFAddressUtil.params.toptree_code;
        var data = {toptree_code: toptree_code};
        var ajax = $.ajax({
            url: HFAddressUtil.URL.getProvince(),
            type: 'POST',
            async:false,
            data: data,
            success: function (res) {
                $('#' + HFAddressUtil.params.province_id).empty();
                $('#' + HFAddressUtil.params.province_id).append("<option value=''>请选择省份</option>")
                var result = res.result; //[]
                for (var i in result) {
                    var area = result[i];
                    $('#' + HFAddressUtil.params.province_id).append("<option value=" + area.id + ">" + area.node_name + "</option>");
                }
                if (attachFunction)
                    attachFunction();
            }
        });
        return ajax;
    },
    loadCity: function (attachFunction) { //加载城市
        var city_id = HFAddressUtil.params.city_id;
        $('#' + city_id).empty();
        $('#' + city_id).append("<option value=''>请选择市级</option>")
        var tree_id = $("#" + HFAddressUtil.params.province_id).find("option:selected").attr('value');
        if (!tree_id || !city_id) {
            //alert("province_id or city_id is null");
            return $.when();
        }
        var data = {tree_id: tree_id};
        var ajax = $.ajax({
            url: HFAddressUtil.URL.getCityList(),
            type: 'POST',
            async:false,
            data: data,
            success: function (res) {
                var result = res.result; //[]
                for (var i in result) {
                    var area = result[i];
                    $("#" + city_id).append("<option value=" + area.id + ">" + area.node_name + "</option>");
                }
// console.log("城市ajax完成");
                if (attachFunction)
                    attachFunction();
            }
        });
        return ajax;
    },
    loadSubcity: function (attachFunction) {
        var subcity_id = HFAddressUtil.params.subcity_id;
        $('#' + subcity_id).empty();
        $('#' + subcity_id).append("<option value=''>请选择子市级</option>")
        var tree_id = $("#" + HFAddressUtil.params.city_id).find("option:selected").attr('value');
// console.log("子城市ajax开始, tree_id:" + tree_id);
        if (!tree_id || !subcity_id) {
            //alert("city_id or subcity_id is null");
            return $.when();
        }
        var data = {tree_id: tree_id};
        var ajax = $.ajax({
            url: HFAddressUtil.URL.getSubcityList(),
            type: 'POST',
            async:false,
            data: data,
            success: function (res) {
                var result = res.result; //[]
                for (var i in result) {
                    var area = result[i];
                    $("#" + subcity_id).append("<option value=" + area.id + ">" + area.node_name + "</option>");
                }
                if (attachFunction)
                    attachFunction();
            }
        });
        return ajax;
    },
    selectValue: function () {
        $.each($("select[value]"), function (i, n) {
            $(n).val($(n).attr("value"));
        });
    },
}
/*
//html
<th width="135">供应商地址</th>
<td colspan = "3">
    <select name="provider_province" id="provider_province" class="form-control col-sm-4 col-hf-100-30 " value="${provider.provider_province}">
    </select>
    <select name="provider_city" id="provider_city" class="form-control col-sm-4 col-hf-100-20 " value="${provider.provider_city}">
    </select>
    <select name="provider_subcity" id="provider_subcity" class="form-control col-sm-4 col-hf-100-20 " value="${provider.provider_subcity}">
    </select>
</td>

//js
HFAddressUtil.init("${AreaType.CHINA.getKey() }", "provider_province", "provider_city", "provider_subcity");


//java
public enum AreaType {
	CHINA("001", "国内"),
	ABROAD("002", "国外");
    .......
}
 */